package com.zjy.service.impl;

import com.zjy.bean.ArticleComment;
import com.zjy.bean.Goods;
import com.zjy.bean.User;
import com.zjy.dao.*;
import com.zjy.dto.ArticleCommentDTO;
import com.zjy.service.GoodsCommentService;
import com.zjy.utils.ObjectUtils;
import com.zjy.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/4/1.
 */
@Service
public class GoodsCommentServiceImpl implements GoodsCommentService {
    @Resource
    private ArticleCommentMapper articleCommentMapper;
    @Resource
    private ArticleDetailMapper articleDetailMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private GoodsMapper goodsMapper;


    /**
     * 新增评论
     * @param articleComment
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addComment(ArticleComment articleComment) {
        //插入商品评论
        String id = UUIDUtils.getUUID();
        articleComment.setId(id);
        articleCommentMapper.insertSelective(articleComment);

        //增加对应商品评论数
        String goodsId = articleComment.getArticleId();
        goodsMapper.updateCommentNumByGoodsId(goodsId,1);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteCommentByCommentId(String commentId) {
        //减少商品的评论数
        Goods goods = goodsMapper.selectGoodsByCommentId(commentId);
        String goodsId = null;
        if (goods != null){
            goodsId = goods.getId();
        }
        //删除评论
        articleCommentMapper.deleteCommentByCommentId(commentId);
        return goodsMapper.updateCommentNumByGoodsId(goodsId,-1);
    }

    @Override
    public List<ArticleCommentDTO> selectCommentByArticleId(String goodsId) {
        List<ArticleCommentDTO> articleCommentDTOList = new ArrayList<ArticleCommentDTO>();//评论树列表
        List<ArticleComment> articleCommentList = articleCommentMapper.selectCommentByArticleId(goodsId);//评论列表
        if(!ObjectUtils.isNullOrEmpty(articleCommentList) && articleCommentList.size() > 0){
            for (ArticleComment articleComment : articleCommentList) { //将评论列表放入评论树列表中
                User user = userMapper.selectUserById(articleComment.getUserId());
                String headPortraitPath = null;
                String userName = null;
                if(user != null){
                    headPortraitPath = user.getHeadPortraitPath();
                    userName = user.getUserName();
                }
                ArticleCommentDTO articleCommentDTO = new ArticleCommentDTO();
                articleCommentDTO.setArticleComment(articleComment);
                articleCommentDTO.setHeadPortraitPath(headPortraitPath);
                articleCommentDTO.setUserName(userName);
                articleCommentDTOList.add(articleCommentDTO);
            }
        }
        List<ArticleCommentDTO> articleCommentDTOList1 = toRecursionList(null,articleCommentDTOList);
        return articleCommentDTOList1;
    }

    //递归获取文章评论树
    public List<ArticleCommentDTO> toRecursionList(ArticleCommentDTO dto, List<ArticleCommentDTO> commentDtoList){
        if (ObjectUtils.isNullOrEmpty(commentDtoList)){
            return null;
        }
        if (dto==null){
            //先拿主评论
            List<ArticleCommentDTO> fartherList=new ArrayList<>();
            for (ArticleCommentDTO articleCommentDTO : commentDtoList) {
                if (articleCommentDTO.getArticleComment().getType() == 0){
                    fartherList.add(articleCommentDTO);
                }
            }
            //递归查询子评论集合
            for (ArticleCommentDTO fartherDto : fartherList) {
                List<ArticleCommentDTO> sonList = toRecursionList(fartherDto, commentDtoList);
                fartherDto.setSonCommentList(sonList);
            }
            return fartherList;
        }else {
            //拿子评论集合
            List<ArticleCommentDTO> sonList=new ArrayList<>();
            for (ArticleCommentDTO articleCommentDTO : commentDtoList) {
                if (dto.getArticleComment().getId().equals(articleCommentDTO.getArticleComment().getLastCommentId())){
                    sonList.add(articleCommentDTO);
                }
            }
            //设置子评论集合
            //dto.setSonCommentList(sonList);
            //递归查询并设置子评论集合
            for (ArticleCommentDTO sonDto : sonList) {
                List<ArticleCommentDTO> nextSonList = toRecursionList(sonDto, commentDtoList);
                sonDto.setSonCommentList(nextSonList);
            }
            return sonList;
        }
    }
}
